{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"Variable_11/read:0\", shape=(1, 2), dtype=float32)\n",
      "[[ 2.]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "a = 3\n",
    "# Create a variable.\n",
    "w = tf.Variable([[0.5,1.0]])\n",
    "x = tf.Variable([[2.0],[1.0]]) \n",
    "\n",
    "y = tf.matmul(w, x)  \n",
    "\n",
    "\n",
    "#variables have to be explicitly initialized before you can run Ops\n",
    "init_op = tf.global_variables_initializer()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init_op)\n",
    "    print (y.eval())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# float32\n",
    "tf.zeros([3, 4], int32) ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\n",
    "\n",
    "# 'tensor' is [[1, 2, 3], [4, 5, 6]]\n",
    "tf.zeros_like(tensor) ==> [[0, 0, 0], [0, 0, 0]]\n",
    "tf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]\n",
    "\n",
    "# 'tensor' is [[1, 2, 3], [4, 5, 6]]\n",
    "tf.ones_like(tensor) ==> [[1, 1, 1], [1, 1, 1]]\n",
    "\n",
    "# Constant 1-D Tensor populated with value list.\n",
    "tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]\n",
    "\n",
    "# Constant 2-D tensor populated with scalar value -1.\n",
    "tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]\n",
    "                                              [-1. -1. -1.]]\n",
    "\n",
    "tf.linspace(10.0, 12.0, 3, name=\"linspace\") => [ 10.0  11.0  12.0]\n",
    "\n",
    "# 'start' is 3\n",
    "# 'limit' is 18\n",
    "# 'delta' is 3\n",
    "tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.30886292  3.11809683  3.29861784]\n",
      " [-7.09597015 -1.89811802  1.75282788]]\n",
      "[[3 4]\n",
      " [5 6]\n",
      " [1 2]]\n"
     ]
    }
   ],
   "source": [
    "norm = tf.random_normal([2, 3], mean=-1, stddev=4)\n",
    "\n",
    "# Shuffle the first dimension of a tensor\n",
    "c = tf.constant([[1, 2], [3, 4], [5, 6]])\n",
    "shuff = tf.random_shuffle(c)\n",
    "\n",
    "# Each time we run these ops, different results are generated\n",
    "sess = tf.Session()\n",
    "print (sess.run(norm))\n",
    "print (sess.run(shuff))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 1\n",
      "[ 0.31830382]\n",
      "[ 0.66478765]\n",
      "[ 1.00602591]\n",
      "[ 0.28635645]\n",
      "Session 2\n",
      "[ 0.53769958]\n",
      "[ 0.82813144]\n",
      "[-0.83388585]\n",
      "[ 0.32747623]\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "state = tf.Variable(0)\n",
    "new_value = tf.add(state, tf.constant(1))\n",
    "update = tf.assign(state, new_value)\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    print(sess.run(state))    \n",
    "    for _ in range(3):\n",
    "        sess.run(update)\n",
    "        print(sess.run(state))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model saved in file:  C://tensorflow//model//test\n"
     ]
    }
   ],
   "source": [
    "#tf.train.Saver\n",
    "w = tf.Variable([[0.5,1.0]])\n",
    "x = tf.Variable([[2.0],[1.0]])\n",
    "y = tf.matmul(w, x)\n",
    "init_op = tf.global_variables_initializer()\n",
    "saver = tf.train.Saver()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init_op)\n",
    "# Do some work with the model.\n",
    "# Save the variables to disk.\n",
    "    save_path = saver.save(sess, \"C://tensorflow//model//test\")\n",
    "    print (\"Model saved in file: \", save_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.zeros((3,3))\n",
    "ta = tf.convert_to_tensor(a)\n",
    "with tf.Session() as sess:\n",
    "     print(sess.run(ta))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([ 14.], dtype=float32)]\n"
     ]
    }
   ],
   "source": [
    "input1 = tf.placeholder(tf.float32)\n",
    "input2 = tf.placeholder(tf.float32)\n",
    "output = tf.mul(input1, input2)\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
